﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kunukn.XmlVisual.Core.Extensions;
using Kunukn.XmlVisual.Core.SvgBuilder;
using Kunukn.XmlVisual.Core.Utilities;

namespace Kunukn.XmlVisual.Core.Entities
{
    public class SvgFile
    {        
        private static readonly string NL = Environment.NewLine;
        public static readonly string XmlHeader = "<?xml version='1.0' encoding='utf-8' standalone='yes'?> " + NL;
//        private static readonly string XmlCss = "<?xml-stylesheet href='css/svg.css' type='text/css'?>" + NL;//not used because it is embedded        
        
        private static readonly string DocType =
            "<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' " + NL
            + "'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>" + NL;


        private static readonly string StyleBeg = "<style type='text/css'>"+NL
                                        + "<![CDATA["+NL;
        private static readonly string StyleEnd = NL+"]]>"+NL
                                        + "</style>" + NL + NL;

        private static readonly string ScriptBeg = "<script type='text/ecmascript'>" + NL
                                        + "<![CDATA[" + NL;
        private static readonly string ScriptEnd = NL + "]]>" + NL
                                        + "</script>" + NL + NL;
                            
        public string SvgBeg
        {
            get { return string.Format("<svg version='1.1' baseProfile='full' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' xmlns:ev='http://www.w3.org/2001/xml-events' onload='initApp(evt)'>" + NL); }
//            get { return string.Format("<svg version='1.1' baseProfile='full' width='{0}%' height='{1}%' preserveAspectRatio='none' viewBox='0 0 {2} {3}' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' xmlns:ev='http://www.w3.org/2001/xml-events' onload='initApp(evt)'>{4}", Width, Height, MathTool.Width, MathTool.Height, NL + NL); }
        }
        public string SvgEnd
        {
            get { return NL + "</svg>"; }
        }        

        public string Css { get; private set; }
        public string Js { get; private set; }
        public string Defs { get; private set; }
        public string CoreSvg { get; private set; }

        // svg        
        public string GraphicContent { get; set; }
        
        public string Text { get; set; }

        public SvgFile(UserConfig userConfig)
        {            
            GraphicContent = string.Empty;
            Css = Cache.Instance.Css(userConfig);
            Js = Cache.Instance.Js(userConfig);
            Defs = Cache.Instance.Defs(userConfig);
            CoreSvg = Cache.Instance.CoreSvg;
        }

        public string CreateTest()
        {
            var sb = new StringBuilder();
            sb.Append(@"<?xml version='1.0' encoding='utf-8' standalone='yes'?> " + Environment.NewLine);
            sb.Append(string.Format("<svg version='1.1' baseProfile='full' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' xmlns:ev='http://www.w3.org/2001/xml-events' onload='initApp(evt)'>" + Environment.NewLine));
            sb.Append(@"<text x='30' y='40'>Svg test</text>");            
            sb.Append(@"</svg>");

            return sb.ToString();
        }

        public string CreateFile()
        {
            var sb = new StringBuilder();

            // DocumentData            
            sb.Append(XmlHeader);

            //sb.Append(DocType); //dont include says https://jwatt.org/svg/authoring/
            sb.Append(SvgBeg);
            sb.Append(string.Format("<!-- {0} Generated by {1} - (c) 2011 Kunuk N -->" + NL + NL, DateTime.Now, Config.Version));            
                        

            // js and css
            if (!string.IsNullOrEmpty(Js))
            {
                sb.Append(ScriptBeg);
                sb.Append(Js);
                sb.Append(ScriptEnd);
            }
            if (!string.IsNullOrEmpty(Css))
            {
                sb.Append(StyleBeg);
                sb.Append(Css);
                sb.Append(StyleEnd);
            }


            if (!string.IsNullOrEmpty(GraphicContent))
            {
                sb.Append(NL + "<!-- GraphicContent -->" + NL);
                if (!string.IsNullOrEmpty(Defs))
                    sb.Append(Defs+NL+NL);                
                sb.Append(GraphicContent);                                
                if (!string.IsNullOrEmpty(CoreSvg))
                    sb.Append(CoreSvg + NL + NL);
            }         

            sb.Append(SvgEnd);

//            return sb.ToString().BlingsChange(); // replace all ' to "  DONT USE, BREAKS BECAUSE NESTED "
            return sb.ToString();
        }

        public override string ToString()
        {
            return CreateFile();
        }
    }
}
